package com.wh.service.impl;

import com.wh.dao.BaseDao;
import com.wh.model.BaseModel;
import com.wh.service.IBaseService;
import com.wh.service.MySqlSessionFactory;
import com.legend.utils.*;

import org.apache.commons.lang.NotImplementedException;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public abstract class BaseServiceImp extends MySqlSessionFactory implements IBaseService {
    Logger log = Logger.getLogger(BaseServiceImp.class);

    public ReturnResult selectById(String id) {

        throw new NotImplementedException("BaseServiceImp未实现selectById方法");
    }

    public ReturnResult editByModel(BaseModel model) {
        throw new NotImplementedException("BaseServiceImp未实现editByModel方法");
    }

    public ReturnResult editByMap(Map<String, Object> map) {
        throw new NotImplementedException("BaseServiceImp未实现此方法");
    }

    public ReturnResult insertByModel(BaseModel model) {

        throw new NotImplementedException("BaseServiceImp未实现此方法");
    }

    public ReturnResult insertByMap(Map<String, Object> map) {
        throw new NotImplementedException("BaseServiceImp未实现此方法");
    }

    public ReturnResult selectByMap(Map<String, Object> map) {
        throw new NotImplementedException("BaseServiceImp未实现selectByMap方法");
    }

    public ReturnResult selectByModel(BaseModel model) {
        throw new NotImplementedException("BaseServiceImp未实现selectByModel方法");
    }

    public ReturnResult delByMap(Map<String, Object> map) {
        throw new NotImplementedException("BaseServiceImp未实现delByMap方法");
    }

    public ReturnResult delByModel(BaseModel model) {
        throw new NotImplementedException("BaseServiceImp未实现delByModel方法");
    }

    public ReturnResult audit(Map<String, Object> map) {
        throw new NotImplementedException("BaseServiceImp未实现audit方法");
    }

    public ReturnResult audit(int state, String[] ids) {
        throw new NotImplementedException("BaseServiceImp未实现audit方法");
    }

    public ReturnResult sort(String sorts, String ids) {

        throw new NotImplementedException("BaseServiceImp未实现sort方法");
    }


    public ReturnResult selectByMap(int pageIndex, int pageSize, Map<String, Object> map) {
        throw new NotImplementedException("BaseServiceImp未实现selectByMap方法");
    }
    public int count( Map<String, Object> map) {
        throw new NotImplementedException("BaseServiceImp未实现count方法");
    }

    public ReturnResult delByIds(String ids) {

        throw new NotImplementedException("BaseServiceImp未实现delByIds方法");
    }

    /**
     * 通用排序
     *
     * @param session
     * @param dao
     * @param ids
     * @param sorts
     * @return
     */
    @Override
    public ReturnResult sort(SqlSession session, BaseDao dao, String ids, String sorts) {
        ReturnResult result = new ReturnResult();
        if (!(StringUtils.isEmpty(sorts) || StringUtils.isEmpty(ids))) {
            String[] sortArr = sorts.split(",");
            String[] idArr = ids.split(",");
            try {
                for (Integer i = 0; i < sortArr.length; i++) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("sort", sortArr[i]);
                    map.put("id", idArr[i]);
                    dao.sort(map);
                }
                session.commit();
                result.setMsg(i18nUtils.getVal("100001000"));
                result.setStatus(Status.SUCCESS);
            } catch (Exception e) {
                session.rollback();
            } finally {
                session.close();
            }
        }
        return result;
    }

    /**
     * 修改数据
     *
     * @param dao
     * @return
     */
    public ReturnResult editByModel(BaseDao dao, BaseModel model) {
        ReturnResult result = new ReturnResult();
        model.setUpdateDate(new Date());
        int i = dao.editByModel(model);
        result.setData(i);
        result.setStatus(Status.SUCCESS);
        result.setMsg(i18nUtils.getVal("100001000"));
        return result;
    }

    /**
     * 插入数据
     *
     * @param dao
     * @param model
     * @return
     */
    public ReturnResult insertByModel(BaseDao dao, BaseModel model) {
        log.info("插入数据");
        ReturnResult result = new ReturnResult();
        try {
            model.setCreateDate(new Date());
            model.setUpdateDate(new Date());
            model.setId(StringUtils.getGuid());
            int i = dao.insertByModel(model);
            result.setStatus(i == 1 ? Status.SUCCESS : Status.ERROR);
            result.setData(i);
            result.setMsg(i18nUtils.getVal(i == 1 ? "100001000" : "100002000"));
        } catch (Exception ex) {
            result.setMsg(i18nUtils.getVal("100002000"));
            result.setStatus(Status.EXCEPTION);
            log.error(ex.getMessage());
        }
        return result;
    }

    /**
     * @param dao
     * @param ids
     * @return
     */
    public ReturnResult delByMap(BaseDao dao, String ids) {

        ReturnResult result = new ReturnResult();
        if (!StringUtils.isEmpty(ids)) {
            String[] idArr = ids.split(",");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("idArr", idArr);
            int effect = dao.delByMap(map);
            if (effect > 0) {
                result.setStatus(Status.SUCCESS);
                result.setMsg(i18nUtils.getVal("100001000"));
            }
        }
        return result;
    }

}
